﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DnPakExplorer.Forms;

namespace DnPakExplorer.Controls {
	public partial class ItemSlotPanelCtrl : MyCustomControl {

		public Sprite BackSprite ;//= new Sprite(Properties.Resources.tabcontrol_background, 3, 3, 3, 3);

		protected static PartImage mSlotBackground;
		protected static PartImage mWeanpon2SlotBackground;
		protected static PartImage mTpyeRing;
		protected static PartImage mSelectedImage;
		protected static Image mImageItemTypes;

		Character mCharacter;
		SlotDragAction mDragAction;

		public Character Character {
			get { return mCharacter; }
		}

		static ItemSlotPanelCtrl(){
			mSlotBackground = new PartImage();
			mSlotBackground.ImageSource = Properties.Resources.item_slot_backgrouds;
			mSlotBackground.SourceRect = new Rectangle(0, 0, 69, 69);
			mSlotBackground.DestRect = new Rectangle(0, 0, 69, 69);

			mWeanpon2SlotBackground = new PartImage();
			mWeanpon2SlotBackground.ImageSource = Properties.Resources.uit_info_normal_re_a;
			mWeanpon2SlotBackground.DestRect = new Rectangle(0, 3, 64, 64);
			mWeanpon2SlotBackground.SourceRect = new Rectangle(4, 348, 64, 64);

			mTpyeRing = new PartImage();
			mTpyeRing.ImageSource = Properties.Resources.uit_itemslot_re03_f;
			mTpyeRing.SourceRect = new Rectangle(0, 0, 64, 64);
			mTpyeRing.DestRect = new Rectangle(6, 6, 64, 64);

			mSelectedImage = new PartImage();
			mSelectedImage.ImageSource = Properties.Resources.uit_itemslotselect02;
			mSelectedImage.SourceRect = new Rectangle(0, 0, 64, 64);
			mSelectedImage.DestRect = new Rectangle(6, 6, 64, 64);

			mImageItemTypes = Properties.Resources.item_slot_types;
		}

		protected PartImage GetTypeImage(int forSlotId, Rectangle destRect) {
			PartImage image = new PartImage();
			image.ImageSource = Properties.Resources.item_slot_types;
			image.DestRect = destRect;
			switch (forSlotId) {
			case EquipmentPanel.IS_CASH_HEAD:
			case EquipmentPanel.IS_HEAD:
				image.SourceRect = new Rectangle(0, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_BODY:
			case EquipmentPanel.IS_BODY:
				image.SourceRect = new Rectangle(50, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_LEG:
			case EquipmentPanel.IS_LEG:
				image.SourceRect = new Rectangle(100, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_HAND:
			case EquipmentPanel.IS_HAND:
				image.SourceRect = new Rectangle(150, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_BOOTS:
			case EquipmentPanel.IS_BOOTS:
				image.SourceRect = new Rectangle(200, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_MAIN_WEAPON:
			case EquipmentPanel.IS_MAIN_WEAPON:
				image.SourceRect = new Rectangle(250, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_SECONDARY_WEAPON:
			case EquipmentPanel.IS_SECONDARY_WEAPON:
				image.SourceRect = new Rectangle(300, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_NECKLACE:
			case EquipmentPanel.IS_NECKLACE:
				image.SourceRect = new Rectangle(350, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_EARRING:
			case EquipmentPanel.IS_EARRING:
				image.SourceRect = new Rectangle(400, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_RING1:
			case EquipmentPanel.IS_CASH_RING2:
			case EquipmentPanel.IS_RING1:
			case EquipmentPanel.IS_RING2:
				image.SourceRect = new Rectangle(450, 0, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_WING:
				image.SourceRect = new Rectangle(0, 50, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_TAIL:
				image.SourceRect = new Rectangle(50, 50, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_STAMP:
				image.SourceRect = new Rectangle(100, 50, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_FAIRY:
			case EquipmentPanel.IS_SOURCE:
				image.SourceRect = new Rectangle(150, 50, 50, 50);
				return image;

			case EquipmentPanel.IS_PET:
				image.SourceRect = new Rectangle(50, 100, 50, 50);
				return image;

			case EquipmentPanel.IS_PET_CLOTH1:
			case EquipmentPanel.IS_PET_CLOTH2:
				image.SourceRect = new Rectangle(100, 100, 50, 50);
				return image;

			case EquipmentPanel.IS_RIDE:
				image.SourceRect = new Rectangle(250, 100, 50, 50);
				return image;

			case EquipmentPanel.IS_GLYGH_NORMAL1:
			case EquipmentPanel.IS_GLYGH_NORMAL2:
			case EquipmentPanel.IS_GLYGH_NORMAL3:
			case EquipmentPanel.IS_GLYGH_NORMAL4:
			case EquipmentPanel.IS_GLYGH_NORMAL5:
			case EquipmentPanel.IS_GLYGH_NORMAL6:
			case EquipmentPanel.IS_GLYGH_NORMAL7:
			case EquipmentPanel.IS_GLYGH_NORMAL8:
				image.SourceRect = new Rectangle(300, 50, 50, 50);
				return image;

			case EquipmentPanel.IS_GLYGH_S1:
			case EquipmentPanel.IS_GLYGH_S2:
			case EquipmentPanel.IS_GLYGH_S3:
			case EquipmentPanel.IS_GLYGH_S4:
				image.SourceRect = new Rectangle(250, 50, 50, 50);
				return image;

			case EquipmentPanel.IS_GLYGH_SKILL1:
			case EquipmentPanel.IS_GLYGH_SKILL2:
			case EquipmentPanel.IS_GLYGH_SKILL3:
			case EquipmentPanel.IS_GLYGH_SKILL4:
				image.SourceRect = new Rectangle(350, 50, 50, 50);
				return image;

			case EquipmentPanel.IS_CASH_GLYGH1:
			case EquipmentPanel.IS_CASH_GLYGH2:
			case EquipmentPanel.IS_CASH_GLYGH3:
				image.SourceRect = new Rectangle(450, 50, 50, 50);
				return image;
			case EquipmentPanel.IS_TALISMAN_2001:
			case EquipmentPanel.IS_TALISMAN_2002:
			case EquipmentPanel.IS_TALISMAN_1751:
			case EquipmentPanel.IS_TALISMAN_1752:
			case EquipmentPanel.IS_TALISMAN_1251:
			case EquipmentPanel.IS_TALISMAN_1252:
			case EquipmentPanel.IS_TALISMAN_1001:
			case EquipmentPanel.IS_TALISMAN_1002:
			case EquipmentPanel.IS_CASH_TALISMAN_200:
			case EquipmentPanel.IS_CASH_TALISMAN_175:
			case EquipmentPanel.IS_CASH_TALISMAN_125:
			case EquipmentPanel.IS_CASH_TALISMAN_100:


				return image;
			}
			return image;
		}		

		ItemSlotBox mSingleSeletedItem {
			get { return _SingleSeletedItem; }
			set {
				if (_SingleSeletedItem != value) {
					Graphics g = CreateGraphics();
					if (_SingleSeletedItem != null) {
						_SingleSeletedItem.Selected = false;
						UpdateSlot(_SingleSeletedItem);
					}
					if (value != null) {
						value.Selected = true;
						UpdateSlot(value);
					}
					_SingleSeletedItem = value;
				}
			}
		}
		ItemSlotBox _SingleSeletedItem;

		public void UpdateSlot(Graphics g, ItemSlotBox slot) {
			g.SetClip(slot.Bounds);
			DrawBackgroud(g);
			slot.OnDraw(g);
		}

		public void UpdateSlot(ItemSlotBox slot) {
			UpdateSlot(CreateGraphics(), slot);
		}

		public ItemSlotPanelCtrl() {
			InitializeComponent();
			SetStyle(ControlStyles.DoubleBuffer
				| ControlStyles.AllPaintingInWmPaint
				| ControlStyles.UserPaint, true);
			this.Disposed += ItemSlotPanelCtrl_Disposed;
		}

		void ItemSlotPanelCtrl_Disposed(object sender, EventArgs e) {
			Cleanup();
		}

		public virtual void Init(Character character) {
			mCharacter = character;
			mCharacter.OnCharacterClosed += mCharacter_OnCharacterClosed;
			this.ForAllView<ItemSlotBox>(slot => {
				if (slot != null) {
					slot.TargetSlot = (character.EquipmentPanel as EquipmentPanel).GetSlot<IItemSlot>(slot.TargetSlotId);
					slot.TargetSlot.Tag = slot;
					slot.TargetSlot.OnItemChanged += OnItemSlotChanged;
				}
			});
		}

		void mCharacter_OnCharacterClosed(object sender, EventArgs e) {
			Cleanup();
			mCharacter.OnCharacterClosed -= mCharacter_OnCharacterClosed;
			Form form = FindForm();
			if (form is PanelForm) {
				form.Close();
			}
		}

		public virtual void Cleanup() {
			this.ForAllView<ItemSlotBox>(slot => {
				slot.SeletedImage = null;
				slot.TypeImage = null;
				slot.Background = null;
				if (slot.TargetSlot != null) {
					if (slot == slot.TargetSlot.Tag)
						slot.TargetSlot.Tag = null;
					slot.TargetSlot.OnItemChanged -= OnItemSlotChanged;
				}
			});
		}

		public void AddSlot(ItemSlotBox slot) {
			if (!ContainsView(slot)) {
				if (mCharacter != null) {
					slot.TargetSlot = (mCharacter.EquipmentPanel as EquipmentPanel).GetSlot<IItemSlot>(slot.TargetSlotId);
					slot.TargetSlot.Tag = slot;
					slot.TargetSlot.OnItemChanged += OnItemSlotChanged;
				}
				AddView(slot);
			}
			
		}


		private void OnItemSlotChanged(IItemSlot slot, IUserItem oldItem) {
			UpdateSlot((ItemSlotBox)slot.Tag);
		}

		private void EquipmentPanelCtrl_Load(object sender, EventArgs e) {

		}

		private void DrawBackgroud(Graphics g) {
			if (BackSprite != null) {
				BackSprite.Draw(g, ClientRectangle);
			} 
		}

		private void EquipmentPanelCtrl_Paint(object sender, PaintEventArgs e) {
			Graphics g = e.Graphics;

			DrawBackgroud(g);

		}

		private void EquipmentPanelCtrl_MouseMove(object sender, MouseEventArgs e) {
			if (mDragAction == null) {
				
			} else {
				mDragAction.Slot.Bounds = new Rectangle(
					mDragAction.SlotOrgPosition.X + e.X - mDragAction.StartPosition.X,
					mDragAction.SlotOrgPosition.Y + e.Y - mDragAction.StartPosition.Y,
					mDragAction.Slot.Bounds.Width,
					mDragAction.Slot.Bounds.Height
					);
				Invalidate();
			}
		}

		private void ItemSlotPanelCtrl_MouseDown(object sender, MouseEventArgs e) {
			if (e.Button == System.Windows.Forms.MouseButtons.Left) {
				this.ForEachView<ItemSlotBox>(slot => {
					if (slot.Bounds.Contains(e.Location)) {
						mDragAction = new SlotDragAction();
						mDragAction.Slot = slot;
						mDragAction.StartPosition = e.Location;
						mDragAction.SlotOrgPosition = slot.Bounds.Location;
						this.Capture = true;
						return true;
					}
					return false;
				});
			}
		}

		private void ItemSlotPanelCtrl_MouseUp(object sender, MouseEventArgs e) {
			if (mDragAction == null) {
				if (e.Button == System.Windows.Forms.MouseButtons.Right) {
					ItemSlotBox select = null;
					this.ForEachView<ItemSlotBox>(slot => {
						if (slot.Bounds.Contains(e.Location)) {
							select = slot;
							return true;
						}
						return false;
					});
					if (select != null && select.TargetSlot != null) {
						if (select.TargetSlot.Item != null) {
							IUserItem item = mCharacter.EquipmentPanel.Uneuip(select.TargetSlotId);
							mCharacter.BagPanel.AddItem(item);
							UpdateSlot(select);
						}
					}
				}
			}
			if (e.Button == System.Windows.Forms.MouseButtons.Left) {
				mDragAction = null;
				this.Capture = false;
			} 
		}

		class SlotDragAction {
			public Point SlotOrgPosition;
			public Point StartPosition;
			public ItemSlotBox Slot;
		}

		protected virtual void SaveLayout(){
			string s = "";
			int i = 0;
			this.ForAllView<ItemSlotBox>(slot => {
				s += "Slot("+(++i)+") = "+slot.Bounds.X+","+slot.Bounds.Y+"\n";
			});
			MessageBox.Show(s);
			Clipboard.SetDataObject(s, true);
		}

		private void ItemSlotPanelCtrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
			if (e.KeyCode == Keys.Enter) {
				SaveLayout();
			}
		}

		

		
	}
}
